package com.example.demo.config.interceptor;

import com.example.demo.annotation.ValidateLogin;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import java.lang.reflect.Method;

public class ValidateLoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        // 获取拦截到的方法
        Method method = handlerMethod.getMethod();
        // 获取方法到的ValidateLogin注解
        ValidateLogin validateLogin = method.getAnnotation(ValidateLogin.class);
        if (validateLogin == null) {
            // 未获取到登录鉴权注解，说明不需要拦截，直接放过
            return true;
        }
        return true;
    }
}
